<template>
  <div style="padding: 5px;">
    <el-card shadow="never">
      <template #header>
        <div style="display: flex;">
          <div style="flex: 1;">请扫描配送二维码</div>
<!--          <div><lbtn >手工查询单据</lbtn ></div>-->
          <div >
            <lbtn  ref="bref" size="large" type="primary" @click="clear">清空</lbtn >
          </div>
        </div>
      </template>
<!--      <el-empty description="请扫描配送单二维码" />-->
      <el-container>
        <el-aside width="400px">
          <t-grid :data-list="psList" size="large" :show-footer="false" :show-page="false" :offset-height="10" :fields="cols1" :remote-sort="false" />
        </el-aside>
        <el-main style="padding: 0;">
          <div style="margin-left: 10px;">
            <t-grid :show-footer="false" size="large" :show-page="false" :offset-height="10" :fields="cols2" :data-list="goods" :remote-sort="false" />
          </div>
        </el-main>
      </el-container>
    </el-card>
  </div>
</template>
<script>
// 扫码统计
import JGrid from "@/lui/JGrid.vue";
import tool from "@/utils/tool";
export default {
  name: 'smtjm',
  components: {JGrid},
  props: {
    showClose: {type: Boolean, default: false}
  },
  data () {
    return {
      offsetH: 300,
      list: [],
      cols1: [
        {type: 'index', width: 40},
        {field: 'bills', title: '票号', width: 110,},
        {field: 'licence', title: '车辆', width: 120,align: 'center',},
        {field: 'state', title: '状态',align: 'center' },
      ],
      cols2: [
        {type: 'index', width: 40},
        {field: 'invName', title: '商品名称', },
        {field: 'spec', title: '规格', width: 60,align: 'center',},
        {field: 'unit', title: '单位', width: 60,align: 'center',},
        {field: 'qty', title: '数量',align: 'right' ,width: 80},
        {field: 'assQty', title: '辅助数量',align: 'right' ,width: 130},
      ],
    }
  },
  computed: {
    gh () {
      return window.innerHeight - this.offsetH
    },
    psList () {
      let ret = []
      for (let i = 0; i < this.list.length; i++) {
        ret.push({
          bills: this.list[i].billNo,
          licence: this.list[i].licence,
          state: this.list[i].state,
        })
      }
      ret = tool.xe.orderBy(ret, [['qty', 'desc']])
      return ret
    },
    goods () {
      let ret = []
      let list = []
      this.list.map(row => {
        row.goods.map(r => {
          list.push(r)
        })
      })
      let gp = tool.xe.groupBy(list, 'invId')
      for(let i in gp) {
        let good = {
          invId: gp[i][0].invId,
          invName: gp[i][0].invName,
          spec: gp[i][0].spec,
          unit: gp[i][0].unit2?gp[i][0].unit2:gp[i][0].unit1,
          unit1: gp[i][0].unit1,
          unit2: gp[i][0].unit2,
          qty: tool.xe.sum(gp[i], 'qty'),
          assQty: ''
        }
        good.assQty = tool.toAssQty(good)
        ret.push(good)
      }
      return ret
    }
  },
  methods: {
    close () {
      this.$router.go(-1)
    },
    scanPs ({code}) {
      let arr = code.split('-')
      let billId = arr[1]
      if (Number(arr[0]) !== 180001) {
        return tool.err('非配送票据')
      }
      if (Number(arr[0]) === 180001) {
        tool.post('scan/findPsBill', {scanInfo: code}).then(ret => {
          if (ret.stat && ret.data) {
            for (let i = 0; i < this.list.length; i++) {
              if(this.list[i].id === ret.data.id) {
                return
              }
            }
            this.list.push(ret.data)
          }
        })
      }
    },
    clear () {
      this.$refs.bref.$el.blur()
      this.list = []
    }
  }
}
</script>